package com.google.code.garbagecan.ejb3study.sessionbean.singleton;

import javax.ejb.Lock;
import javax.ejb.Singleton;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static javax.ejb.LockType.READ;
import static javax.ejb.LockType.WRITE;

@Singleton
@Lock(READ)
public class ComponentRegistry {
	
	private final Map<Class, Object> components = new HashMap<Class, Object>();

	public <T> T getComponent(Class<T> type) {
		return (T) components.get(type);
	}

	public Collection<?> getComponents() {
		return new ArrayList(components.values());
	}

	@Lock(WRITE)
	public <T> T setComponent(Class<T> type, T value) {
		return (T) components.put(type, value);
	}

	@Lock(WRITE)
	public <T> T removeComponent(Class<T> type) {
		return (T) components.remove(type);
	}
}
